import * as orderService from '../services/order';

import { Toast } from 'antd-mobile'
import { Link, browserHistory } from 'dva/router';


export default {

  namespace: 'order',

  state: {
    orderList: null,
    orderDetail: null,
    orderCount: null
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
    }
  },

  effects: {
    *paySuccessBack( { payload }, { call, put }) {
      const { data } = yield call(orderService.paySuccessBack, payload) 
      if(data && data.status === 1) {
        Toast.info('支付成功', 1)
        // browserHistory.push(`/cashier?orderSn=${data.result.orderSn}&orderAmount=${data.result.orderAmount}&orderId=${data.result.orderId}`)
      } else {
        Toast.info(`${data && data.msg || '支付失败'}`);
      }
    },
    *orderCount( { payload }, { call, put }) {
      const { data } = yield call(orderService.orderCount, payload) 
      yield put({
        type: 'saveOrderCount',
        payload: {
          orderCount: data && data.result || {}
        }
      })
    },  
    *payWeixinpay({ payload } , { call, put }) {
      const { data } = yield call(orderService.payWeixin, payload)
      
      // call js WeixinJSBridge.invoke
      if(WeixinJSBridge) {
          let invokeObj = data && data.result || {}
          WeixinJSBridge.invoke(
           'getBrandWCPayRequest', {
              ...invokeObj,
               "signType": "MD5",         //微信签名方式：     
               "paySign": invokeObj.sign
           },
           function(res){
              alert(res)     
               if(res.err_msg == "get_brand_wcpay_request：ok" ) {

               }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。 
           }
        );   
      }
      
    },
    *payAlipay({ payload }, { call, put }) {
      const { data } = yield call(orderService.payAlipay, payload)
      window.location.href = data.url
    },
    *payDoPay({ payload }, { call, put }) {
      const { data } = yield call(orderService.payDoPay, payload)

      // call js WeixinJSBridge.invoke
      if(WeixinJSBridge) {
          let invokeObj = JSON.parse(data.result) || {}
          WeixinJSBridge.invoke(
           'getBrandWCPayRequest', {
              ...invokeObj,
               "signType": "MD5",         //微信签名方式：     
               "paySign": invokeObj.sign
           },
           function(res){
              debugger     
               if(res.err_msg == "get_brand_wcpay_request：ok" ) {

               }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。 
           }
        );   
      }
    },
    *submitOrder({ payload }, { call, put }) {
      const { data } = yield call(orderService.submitOrder, payload.searchParam) 

      if(data && data.status === 1) {
        Toast.info('提交订单成功', 1)
        browserHistory.push(`/cashier?orderSn=${data.result.orderSn}&orderAmount=${data.result.orderAmount}&orderId=${data.result.orderId}`)

        payload.callback && payload.callback()
      } else {
        Toast.info(`${data && data.msg || '提交订单失败，请稍后再试'}`);
      }
    },
    *getOrderList({ payload }, { call, put }) {
      yield put({
        type: 'emptyOrderList'
      })
      const { data } = yield call(orderService.getOrderList, payload) 

      yield put({
        type: 'saveOrderList',
        payload: {
          orderList: data && data.result || []
        }
      })
    },
    *getOrderDetail({ payload }, { call, put }) {
      const { data } = yield call(orderService.getOrderDetail, payload) 
      yield put({
        type: 'saveOrderDetail',
        payload: {
          orderDetail: data && data.result || {}
        }
      })
    }
  },

  reducers: {
    saveOrderList(state, { payload: { orderList }}) {
      return { ...state, orderList }
    },
    saveOrderDetail(state, { payload: { orderDetail }}) {
      return { ...state, orderDetail }
    },
    emptyOrderList(state) {
      return { ...state, orderList: null } 
    },
    saveOrderCount(state, { payload: { orderCount }}) {
      return { ...state, orderCount }
    }
  }

};
